<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\Responder;
use StudyBuddy\WebPage;
use StudyBuddy\Paginator;
use StudyBuddy\Template\Urhere;
use StudyBuddy\Request;

/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Deletebadges extends WebPage {

    protected $layoutID = 1;
    protected $qtab = 'questions';
    protected $oCursor;
    protected $Html;
    protected $bid;
    protected $submitedit;
    protected $permission = 'delete_answer';

    protected function main() {

        $this->init()
                ->getCursor()
                ->renderHtml();

        $this->setTitle()
                ->makeTopTabs()
                ->setUsers();
    }

    protected function setTitle() {
        $title = 'Delete Badges';
        $this->aPageVars['title'] = $title;
        $this->aPageVars['qheader'] = '<h1>' . $title . '</h1>';

        return $this;
    }

    protected function init() {

        $this->getUser();

        return $this;
    }

    protected function getUser() {

        $uid = $this->oRequest->get('uid', 'i', null);
        if ($uid && ($uid !== $this->oRegistry->Viewer->getUid())) {
            $this->checkAccessPermission($permission);
            $this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
        } else {
            $this->oUser = $this->oRegistry->Viewer;
        }

        return $this;
    }

    protected function makeTopTabs() {
        $guest = null;
        if($this->oRegistry->Viewer->isGuest()) {
            $guest['link_ask'] = '#'; // 13
            $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
        }
        d('cp');
        $tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
        $this->aPageVars['topTabs'] = $tabs;

        return $this;
    }

    protected function getCursor() {

        $this->bid = $this->oRegistry->Request->get('bid', 'i', null);
        $where = array('id' => $this->bid);
        $this->oRegistry->Mongo->BADGES->remove($where);

        return $this;
    }

    protected function renderHtml() {

        $this->Html = '<div class="ctl-ct badgeswrap"><h3>Delete Badges</h3>';
        $this->Html .= '<div style="padding:50px;">';
        $this->Html .= '<span>Delete sucessful!</span><span><a href="/managebadges">Back to manage badges.</a></span>';
        $this->Html .= '</div></div>';

        return $this;
    }

    protected function setUsers() {

        $this->aPageVars['body'] .= $this->Html;

        return $this;
    }

}
